package com.aramco.fion.controller
{
	import com.aramco.fion.event.EmailEvent;
	import com.aramco.fion.model.AppModel;
	import com.aramco.fion.model.FlexConstants;
	import com.aramco.fion.service.AppDelegate;
	import com.aramco.fion.service.UserService;
	import com.aramco.fion.util.HTTPUtil;
	import com.aramco.fion.vo.User;
	
	import flash.display.DisplayObject;
	import flash.events.IEventDispatcher;
	
	import mx.controls.Alert;
	import mx.core.IFlexDisplayObject;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.ResultEvent;
	
	import org.swizframework.events.BeanEvent;
	import org.swizframework.utils.services.ServiceHelper;
	
	
	
	public class AppController
	{

		[Inject]
		public var model : AppModel;
		
		[Inject]
		public var serviceRequestUtil:ServiceHelper;
		
		[Inject]
		public var delegate:AppDelegate;
		
		private static var disptatcher:IEventDispatcher;
		
		
		
		[PostConstruct]		
		public function preInitialize():void
		{
			if( HTTPUtil.getHostName() != "")
			{
			 model.completeURL = 'http://' + HTTPUtil.getHostName() +
					':' + HTTPUtil.getPort() ;
			}
			else
			{
				model.completeURL = 'http://dvb.aramco.com.sa:1080';
			}
		}
		
		
		[EventHandler(event = "EmailEvent.SEND_EMAIL_EVENT")]
		public function sendEmailHandler(event:EmailEvent):void
		{
			var call:AsyncToken = delegate.sendEmail(event.parm);
			serviceRequestUtil.executeServiceCall( call, loadResultHandler, loadFaulttHandler );
		}
		
		
		//------------ Handel Result ---------------------------------
		private function loadResultHandler(data: * ):void 
		{
			//parse XML result
			trace(data);
			var result:XML = new XML(data.result);
			//set flags isSuccessful and isCompleted to true
			if(result.ERR_MSG.toString() == '')//success
				Alert.show("Email Sent");
		}
		
		//------------ Handel Result ---------------------------------
		private function loadFaulttHandler(data: * ):void
		
		{
			//parse XML result
			trace(data);
			var result:XML = new XML(data);
			//set flags isSuccessful and isCompleted to true

			if(result.ERR_MSG.toString() == '')//success
				Alert.show("Email Failed");
		}
		
	}
}